﻿/// <reference path="photo.blog.models.ts" />
/// <reference path="../typings/knockout.mapping/knockout.mapping.d.ts" />

module PhotoBlog.Api
{

    export class DataLoader
    {
        load: (data: any, success: (result: any) => void) => void;
        constructor(url: string, httpMethod: string)
        {
            this.load = (data, success) =>
            {
                var jsonData = data == null ? null : ko.mapping.toJSON(data);
                var settings: JQueryAjaxSettings = {
                    url: url,
                    dataType: "json",
                    type: httpMethod,
                    data: jsonData,
                    contentType: "application/json; charset=utf-8",
                    error: () => { alert('При запросе данных произошла ошибка'); },
                    beforeSend: function ()
                    {
                        jQuery("#photos-loader").show();
                        jQuery("#loader-image").show();
                    },
                    success: function (data)
                    {
                        jQuery("#loader-image").hide();
                        success(data);
                    },
                    complete: function () { }
                };
                jQuery.ajax(settings);
            };
        }
    }

    export class AsyncClient
    {

        static Current = new AsyncClient();

        GetPhotos = (data: PhotoBlog.Models.GetPhotosParams, success: (result: PhotoBlog.Models.MainViewM) => void) =>
        {
            var dataLoader = new DataLoader("Home/Photos", "Post");
            dataLoader.load(data, (result) =>
            {
                success(result);
            });
        };

        GetAlbums = (success: (result: PhotoBlog.Models.AlbumViewM) => void) =>
        {
            var dataLoader = new DataLoader("albums", "Post");
            dataLoader.load(null, (result) =>
            {
                success(result);
            });
        };

        GetAlbumPhotos = (id: number, success: (result: PhotoBlog.Models.AlbumPhotosViewM) => void) =>
        {
            var dataLoader = new DataLoader("/home/LoadAlbum/" + id, "Post");
            dataLoader.load(null, (result) =>
            {
                success(result);
            });
        };

        GetDayPhoto = (data: PhotoBlog.Models.GetPhotosParams, success: (result: PhotoBlog.Models.DayPhotoM) => void) =>
        {
            var dataLoader = new DataLoader("/home/DayPhoto", "Post");
            dataLoader.load(data, (result) =>
            {
                success(result);
            });
        };

        SavePhotos = (photo: PhotoBlog.Models.PhotoM, success: (result: boolean) => void) =>
        {
            var dataLoader = new DataLoader("/home/SavePhoto", "Post");
            dataLoader.load(photo, (result) =>
            {
                success(result);
            });
        };
    }
}